import {onMounted, ref} from "vue";
import {onBeforeRouteUpdate, useRoute} from "vue-router";
import {getCategoryAPI} from "@/apis/category.js";

export function useCategory(){
    const categoryData = ref({})
    const route = useRoute()
    const getCategory = async (to = route.params.id) =>{
        const res  = await getCategoryAPI(to)
        categoryData.value = res.result
    }

    onMounted(()=>getCategory())

    onBeforeRouteUpdate((to)=>{
        console.log('路由变化了')
        getCategory(to.params.id)
    })
    return{
        categoryData
    }
}